﻿/*
 * Erstellt mit SharpDevelop.
 * Benutzer: Kai Patrick
 * Datum: 17.03.2008
 * Zeit: 13:54
 * 
 * Sie können diese Vorlage unter Extras > Optionen > Codeerstellung > Standardheader ändern.
 */

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;

namespace Gamadus.Designer
{
	/// <summary>
	/// Description of MainForm.
	/// </summary>
	public partial class MainForm : Form
	{
		Splitter splitterMain;
		
		public MainForm()
		{
			
			splitterMain = new Splitter();
            splitterMain.Name = "splitterMain";
            splitterMain.Dock = DockStyle.Left;
            // Set the minimum size the MDI area can be sized to.
            splitterMain.MinExtra = 100;
            // Set the minimum size the TreeView control can be sized to.
            splitterMain.MinSize = 75;
            this.Controls.Add(splitterMain);
			//
			// The InitializeComponent() call is required for Windows Forms designer support.
			//
			InitializeComponent();
			
			ResourceMgr.Instance.Init(this);
			ActionSetMgr.Instance.Init();
			
			//Add an empty image to the imageList
			imageListResources.Images.Add("none",new Bitmap(16,16));
			List<string> typeNames = new List<string>(Enum.GetNames(typeof(ResourceType)));
			foreach(TreeNode node in treeViewResources.Nodes)
			{
				if(node.Level == 0)
				{
					if(typeNames.Contains(node.Name.Substring(4)))
						ResourceMgr.Instance.TypeNodes.Add((ResourceType)Enum.Parse(typeof(ResourceType),node.Name.Substring(4)), node);
				}
				if(node.Name != "nodeSettings")
					node.NodeFont = new Font(treeViewResources.Font, FontStyle.Bold);
				else
				{
					node.ImageKey = "game-settings";
					node.SelectedImageKey = node.ImageKey;
				}
			}
			
		}
		
		void TreeViewResourcesAfterCollapse(object sender, TreeViewEventArgs e)
		{
			e.Node.ImageKey = "folder-closed";
			e.Node.SelectedImageKey = e.Node.ImageKey;
		}
		
		void TreeViewResourcesAfterExpand(object sender, TreeViewEventArgs e)
		{
			e.Node.ImageKey = "folder-open";
			e.Node.SelectedImageKey = e.Node.ImageKey;
		}
		
		void TreeViewResourcesBeforeLabelEdit(object sender, NodeLabelEditEventArgs e)
		{
			if(e.Node.Level == 0)
				e.CancelEdit = true;
		}
		
		void TreeViewResourcesAfterLabelEdit(object sender, NodeLabelEditEventArgs e)
		{
			try
			{
				if(!ResourceMgr.Instance.IsValidName(e.Label))
					e.CancelEdit = true;
				else
				{
					ResourceMgr.Instance.Resources[e.Node].Name = e.Label;
					ResourceMgr.Instance.DidChanges = true;
				}
			}
			catch(ArgumentNullException)
			{}
		}
	
		void TreeViewResourcesNodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e)
		{
			if(e.Node.Level != 0 && ResourceMgr.Instance.Resources.ContainsKey(e.Node))
				ResourceMgr.Instance.Resources[e.Node].ShowEditWindow();
		}
		
		#region Resource Creation Events
		void CreateSpriteToolStripMenuItemClick(object sender, EventArgs e)
		{
			ResourceMgr.Instance.Add(ResourceType.Sprite, true);
		}
		
		void CreateBackgroundToolStripMenuItemClick(object sender, EventArgs e)
		{
			ResourceMgr.Instance.Add(ResourceType.Background, true);
		}
		
		void CreateObjectToolStripMenuItemClick(object sender, EventArgs e)
		{
			ResourceMgr.Instance.Add(ResourceType.Object, true);
		}
		
		void CreateRoomToolStripMenuItemClick(object sender, EventArgs e)
		{
			ResourceMgr.Instance.Add(ResourceType.Room, true);
		}
		
		void CreateClassToolStripMenuItemClick(object sender, EventArgs e)
		{
			ResourceMgr.Instance.Add(ResourceType.Class, true);
		}
		#endregion
		
		public void CloseProject()
		{
			//delete resources
			foreach(KeyValuePair<TreeNode, Resource> resource in ResourceMgr.Instance.Resources)
			{
				treeViewResources.Nodes.Remove(resource.Key);
			}
			ResourceMgr.Instance.Resources.Clear();
		}
		
		void MainFormFormClosing(object sender, FormClosingEventArgs e)
		{
			if(ResourceMgr.Instance.editWindows.Count > 0)
				e.Cancel = true;
			else if(!ResourceMgr.Instance.CloseProject(false))
				e.Cancel = true;
		}
		
		void NewToolStripMenuItemClick(object sender, EventArgs e)
		{
			ResourceMgr.Instance.NewProject();
		}
		
		void OpenToolStripMenuItemClick(object sender, EventArgs e)
		{
			ResourceMgr.Instance.OpenProject();
		}
		
		void SaveToolStripMenuItemClick(object sender, EventArgs e)
		{
			ResourceMgr.Instance.SaveProject();
		}
		
		void SaveAsToolStripMenuItemClick(object sender, EventArgs e)
		{
			ResourceMgr.Instance.SaveProjectAs();
		}
		
		void MainFormFormClosed(object sender, FormClosedEventArgs e)
		{
			ResourceMgr.Instance.Clean();
		}
	}
}
